package com.bw.order_module.confirm;

import android.os.Bundle;

import androidx.databinding.ObservableField;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.bw.common.providerservice.CheckLoginApi;
import com.bw.common.utils.ToastShowUtils;
import com.bw.network_sdk.BaseEntity;
import com.bw.order_module.entity.Order;
import com.bw.order_module.entity.OrderEntity;
import com.bw.order_module.entity.OrderGoods;
import com.bw.order_module.entity.PayEntity;
import com.bw.order_module.entity.PayOrderEntity;
import com.bw.order_module.entity.ShipAddress;
import com.bw.order_module.entity.SubOrderEntity;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ConfirmViewModel extends BaseViewModel<ConfirmRespository> {

    public long orderId = 0l;
    public ObservableField<String>totalPrice = new ObservableField<>();
    public ObservableField<List<OrderGoods>>orderGoods = new ObservableField<>();
    private Order order;
    private CheckLoginApi checkLoginApi;

    public void refreshOrderStatues(){
        Map<String,Object>map = new HashMap<>();
        map.put("orderId",orderId);
        respository.requestPayOrder(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    public void subOrderListener(){
        if (order == null)
            return;
        Map<String,Object>map=new HashMap<>();
        map.put("userId",checkLoginApi.getUserId());
        ShipAddress shipAddress = new ShipAddress();
        shipAddress.id = 9;
        shipAddress.ship_is_default = 1;
        shipAddress.ship_user_name = "11";
        shipAddress.ship_address = "11";
        shipAddress.ship_user_mobile = "11";
        shipAddress.user_id = 481;
        order.shipAddress = shipAddress;
        order.orderStatus = 0;//待付款状态
        order.payType = 0;//支付宝支付
        map.put("order",order);
        respository.requestSubOrder(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    public void onResult(BaseEntity entity) {
        if (entity instanceof OrderEntity){
            OrderEntity orderEntity = (OrderEntity) entity;
            order = orderEntity.data;
            orderGoods.set(orderEntity.data.orderGoodsList);
            totalPrice.set(orderEntity.data.totalPrice);
        }else if (entity instanceof SubOrderEntity){
            if (entity.message.equals("订单提交成功")){
                //调用支付接口->获取支付宝支付得签名信息
                Map<String,Object>map=new HashMap<>();
                map.put("totalPrice",totalPrice.get());
                map.put("orderId",orderId);
                respository.requestPay(map)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(this);
            }
        }else if (entity instanceof PayEntity){
            PayEntity payEntity = (PayEntity) entity;
            String paysign = payEntity.data;
            //调用支付宝相关SDK进行支付
            Bundle bundle = new Bundle();
            bundle.putString("paysign",paysign);
            startBundPage("/app/payactivity",bundle);
        }else if (entity instanceof PayOrderEntity){
            ToastShowUtils.showShort(entity.message);
            //刷新购物车列表显示数据

        }
    }

    @Override
    protected void initData() {
        checkLoginApi = (CheckLoginApi) ARouter.getInstance()
                .build("/user/checklogin").navigation();
        //订单详情获取
        Map<String,Object>map=new HashMap<>();
        map.put("orderId",orderId);
        respository.requestDetailOrder(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }
}
